<?php 
namespace App\Controllers; 
 
class Athletes extends BaseController 
{ 
    public function index() 
    { 
         // connect to the model 
         $Athletes = new \App\Models\Athletes(); 
         // retrieve all the records 
         $records = $Athletes->findAll(); 

        $fields = [
            'title' => 'Athletes',
            'heading' => 'Athletes',
            'footer' => 'Copyright ZhouRenke'
        ];
          // get a template parser 
        $parser = \Config\Services::parser(); 
        // tell it about the substitions 
        return  $parser->setData($fields)
                       ->render('templates\top') .
                $parser->setData(['records' => $records]) 
                ->render('Athleteslist').
                $parser->setData($fields)
                       ->render('templates\bottom');
    } 

    public function create($id = NULL) 
    { 
        $fields = [ 
            'title' => 'Athletes',
            'heading' => 'Athletes',
            'footer' => 'Copyright ZhouRenke'
        ]; 
 
        $record = [ 
            'id' => 0, 
            'Name' => '', 
            'Gender' => '', 
            'Date_of_Birth' => '', 
            'Height' => '', 
            'Weight' => '', 
            'Playing_Style' => '', 
            'Place_of_Birth' => '', 
            'image' => '', 
        ]; 
 
        $parser = \Config\Services::parser(); 
        
        return $parser->setData($fields)
        ->render('templates\top') .
            $parser->setData($record) 
                ->render('editAthletes') . 
            $parser->setData($fields)
                   ->render('templates\bottom'); 
    } 

    public function showme($id) 
    { 
        // connect to the model 
        $Athletes = new \App\Models\Athletes(); 
        // retrieve all the records 
        $record = $Athletes->find($id); 

        $table = new \CodeIgniter\View\Table();
        $cardstar = '<div class="content">';
        $cardstop = '</div>';
        $table->setHeading('<p><a href="/Athletes/edit/'.$record['id'].'">Edit</a></p>');
        $table->addRow('Id',$record['id']);
        $table->addRow('Name',$record['Name']);
        $table->addRow('Gender',$record['Gender']);
        $table->addRow('Date_of_Birth',$record['Date_of_Birth']);
        $table->addRow('Height',$record['Height']);
        $table->addRow('Weight',$record['Weight']);
        $table->addRow('Playing_Style',$record['Playing_Style']);
        $table->addRow('Place_of_Birth',$record['Place_of_Birth']);
        $table->addRow('Image','<img src=\\image\\' .$record['image'].'>');

        $template = [
            'table_open' => '<table cellpadding="20px" style="margin-top:30px; margin-bottom:30px;">',
            'cell_start' => '<td style="border: 1px solid #dddddd;">',
            'row_alt_start' => '<tr style="background-color:#dddddd">',
            ];
            $table->setTemplate($template);
            
            $fields = [
                'title' => ''.$record['Name'].'',
                'heading' => ''.$record['Name'].'',
                'footer' => 'Copyright ZhouRenke'
            ];
        $parser = \Config\Services::parser(); 

        return $parser->setData($fields)
        ->render('templates\top') .$cardstar.
            $table->generate() . $cardstop.
        $parser->setData($fields)
        ->render('templates\bottom');

    }
    

    public function edit($id = NULL) 
    { 
        $places = new \App\Models\Athletes();
        $record = $places->find($id);
        $fields = [
            'title' => 'Edit a Athletes',
            'heading' => 'Edit a Athletes',
            'footer' => 'Copyright ZhouRenke'
        ];
        $parser = \Config\Services::parser();
        return $parser->setData($fields)
            ->render('templates\top') .
            $parser->setData($record)
                ->render('editAthletes') .
            $parser->setData($fields)
                ->render('templates\bottom');

    }

    public function handle() 
    { 
        $places = new \App\Models\Athletes();
        helper(['form', 'url']);
        $id = $this->request->getPost('id');
        $record = $places->find($id);
        if ($this->validate($places->validationRules)) {
            if(id==0){
                $places->save([
                'Name' => $this->request->getPost('Name'),
                'Gender' => $this->request->getPost('Gender'),
                'Date_of_Birth' => $this->request->getPost('Date_of_Birth'),
                'Height' => $this->request->getPost('Height'),
                'Weight' => $this->request->getPost('Weight'),
                'Playing_Style' => $this->request->getPost('Playing_Style'),
                'Place_of_Birth' => $this->request->getPost('Place_of_Birth'),
                'image' => $this->request->getPost('image'),
            ]);
            }else{
                $places->save([
                    'id' => $this->request->getPost('id'),
                    'Name' => $this->request->getPost('Name'),
                    'Gender' => $this->request->getPost('Gender'),
                    'Date_of_Birth' => $this->request->getPost('Date_of_Birth'),
                    'Height' => $this->request->getPost('Height'),
                    'Weight' => $this->request->getPost('Weight'),
                    'Playing_Style' => $this->request->getPost('Playing_Style'),
                    'Place_of_Birth' => $this->request->getPost('Place_of_Birth'),
                    'image' => $this->request->getPost('image'),
                ]);
            }
        }
        $fields = [
            'title' => 'Edit a Athletes',
            'heading' => 'Edit a Athletes',
            'footer' => 'Copyright ZhouRenke'
        ];
        $parser = \Config\Services::parser();

        //Top of page
        $returnView = $parser->setData($fields)->render('templates\top');

        //Body of page
        if (!$this->validate($places->validationRules)) {
            $returnView .= $parser->renderString($this->validator->listErrors());
            $returnView .= $parser->setData($this->request->getPost())
                ->render('editAthletes');
        } else {
            $returnView .= $parser->renderString("<p>Success!</p>");
            $returnView .= $parser->setData($this->request->getPost())
                ->render('editAthletes');
        }

        //Bottom of page
        $returnView .= $parser->setData($fields)
            ->render('templates\bottom');

        return $returnView;

    }

}